﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class AddVendor : System.Web.UI.UserControl
    {
        public string filePathVendor;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                // Them du lieu vao cat 1 Them du lieu vao cat 1
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", 0, "selectwhereid");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                ddl_Cate1.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddl_Cate1.Items.Add(lt);
                }
                ddl_Cate1.SelectedIndex = 0;

                txtName.Enabled = false;
                txtDescription.Enabled = false;
                fulImageVendor.Enabled = false;
                btnAddVendor.Enabled = false;

            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtName.Enabled = true;
            txtDescription.Enabled = true;
            fulImageVendor.Enabled = true;
            btnAddVendor.Enabled = true;
        }

        protected void btnAddVendor_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Vendor", "Vendor_ID"));
            if (txtName.Text != "")
            {
                if (fulImageVendor.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImageVendor.FileName))
                    {                        
                            int cate1_id = Convert.ToInt32(ddl_Cate1.SelectedValue.ToString());
                            int idnextvendor = nextid + 1;
                            string linkimage = fulImageVendor.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Vendor/"), linkimage, 300, fulImageVendor.PostedFile.InputStream);
                            filePathVendor = "/Images/Image-Vendor/" + fulImageVendor.FileName;
                            string value = "" + idnextvendor + "," + cate1_id + ",N'" + txtName.Text + "',N'" + filePathVendor + "','" + txtDescription.Text + "'";
                            List<string> values = new List<string>();
                            values.Add(value);
                            DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Vendor", "insertemployee", values, false);
                            Response.Write("<script>alert('Thêm nhà sản xuất thành công...')</script>");                                               
                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng upload logo nhà sản xuất...')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập tên nhà sản xuất...')</script>");
            }
        }
    }
}